import { Component, OnInit } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { DatePipe } from '@angular/common';
import { LocalStorage } from '@core/local.storage';
import { ReuseTabService } from '@delon/abc';
import { Router } from '@angular/router';
import { TctjMonthService } from './tctj_month.service';
import { ExportFileService } from '@core/exportFile';
import { NzMessageService } from 'ng-zorro-antd';
import { environment } from '../../../../../environments/environment';

@Component({
  selector: 'app-tctj-month',
  templateUrl: './tctj_month.component.html',
  styleUrls: ['./tctj_month.component.less', '../tctj.less']
})
export class TctjMonthComponent implements OnInit {
  queryParam: any = {};
  lockHeadScroll = { x: '1500px', y: window.innerHeight - 350 + 'px' };
  main: any = {
    tableData: {
      zhtc: [],
      cztc: [],
      sum: []
    },
    tbsj: '',
    _tbsj: '',
  };

  detail: any = {
    visible: false,
    selectedValue: '20',
    pageIndex: 1,
    pageTotal: 0,
    footer: [{
      _this: this,
      label: '关闭', // 按钮文本
      onClick: () => {
        this.detail.visible = false;
      }
    }],
    showModal() {
      this.visible = true;
    },
    closeModal() {
      this.visible = false;
    },
    selectedChange: () => {
      const param = {};
      Object.assign(param, this.lastDetailParam, { limit: this.detail.selectedValue });
      this.getDetail(param);
    },
    pageIndexChange() {
      const param = {};
      Object.assign(param, this.lastDetailParam, { limit: this.detail.pageIndex });
      this.getDetail(param);
    }
  };

  userInfo = JSON.parse(this.ls.get('loginInfo'));
  isSpinning = false;
  detailLoading = false;
  lastDetailParam: any = {};
  typeSelect = [
    { value: 'line', label: '按线别统计' },
    { value: 'org', label: '按车间统计' },
    { value: 'primary', label: '按提报主体统计' },
    { value: 'level', label: '按维修级别统计' },
    { value: 'win', label: '按天窗类型统计' }
  ];

  constructor(
    private datePipe: DatePipe,
    private tctjMonthService: TctjMonthService,
    public ls: LocalStorage,
    private router: Router,
    private exportFile: ExportFileService,
    public msg: NzMessageService,
    private reuseTabService: ReuseTabService
  ) { }

  ngOnInit(): void {
    this.initMonth();
    this.query();
  }

  initMonth() {
    this.queryParam._monthStr = new Date();
  }

  /**
     * 删除私立字段
     * @param param
     */
  removePrivateFields(param) {
    Object.keys(param).forEach(key => {
      if (key.indexOf('_') == 0) {
        delete param[key];
      }
    });
  }

  formatDate(param) {
    if (param._monthStr) {
      param.monthStr = this.datePipe.transform(param._monthStr, 'yyyy-MM');
    }
  }
  // 跳转到统计页面
  statistics() {
    if (!this.queryParam._monthStr) {
      this.msg.error('请选择月份');
      return;
    }
    if (!this.queryParam.type) {
      this.msg.error('请选择类型');
      return;
    }
    const param = {
      month: this.datePipe.transform(this.queryParam._monthStr, 'yyyy-MM'),
      type: this.queryParam.type
    };
    this.router.navigate(['tctj/statistics/' + param.type], {
      queryParams: param
    });
  }

  query() {
    this.isSpinning = true;
    const param: any = {};
    Object.assign(param, this.queryParam);
    this.formatDate(param);
    this.removePrivateFields(param);

    this.tctjMonthService.query(param).subscribe(res => {
      if (!res.code) {
        this.isSpinning = false;
        if (this.queryParam._monthStr) {
          this.main.tbsj = this.datePipe.transform(this.queryParam._monthStr, 'yyyy年MM月');
          if (this.main._tbsj) {
            this.main._tbsj = this.main.tbsj.replace('年', '').replace('月', '');
          }
        }
        const data = res;
        const zhtc = [];
        const cztc = [];
        const sum = [];

        data.forEach(row => {
          switch (row.skywinType) {
            case '综合天窗':
              zhtc.push(row);
              break;
            case '垂直天窗':
              cztc.push(row);
              break;
            case '总计':
              sum.push(row);
              break;
          }
        });
        this.main.tableData.zhtc = zhtc;
        this.main.tableData.cztc = cztc;
        this.main.tableData.sum = sum;
      }
    });
  }

  queryDetail(row, detailType) {
    if (!this.main._tbsj || !detailType) {
      return;
    }
    this.detail.visible = true;
    this.detail.tableData = [];
    const param = {
      skywinType: row.skywinType || '',
      line: row.line || '',
      monthStr: this.main._tbsj,
      detailType,
      page: this.detail.pageIndex,
      limit: this.detail.selectedValue,
    };
    this.getDetail(param);
  }

  getDetail(param) {
    this.detailLoading = true;
    this.tctjMonthService.detail(param).subscribe(res => {
      this.detailLoading = false;
      this.lastDetailParam = param;
      this.detail.tableData = res.data;
      this.detail.pageTotal = res.count;
    });
  }

  export() {
    if (!this.main._tbsj) {
      return;
    }

    this.exportFile.postDown(environment.project + '/skywinStat/monthStatExport', { monthStr: this.main._tbsj }, '天窗统计' + this.main._tbsj + '.xls');
  }
}
